package com.person.monitor;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;

import com.person.monitor.location.BaiduLocation;
import com.person.monitor.location.BaseLocation;

import java.util.Calendar;

public class MonitorTool {
    public static final String WIFI_NONE = "<unknown ssid>";

    float lightLux;
    Context context;
    WifiManager wifiManager;
    SensorManager sensorManager;
    BaseLocation baseLocation;

    public MonitorTool(Context context){
        this.context = context;

        wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        sensorManager = LightSensorUtil.getSenosrManager(context);
        LightSensorUtil.registerLightSensor(sensorManager,lightSensorListener);

        baseLocation = new BaiduLocation(context);
        baseLocation.startLocation();
    }

    public MonitorInfo getMonitorInfo(){
        MonitorInfo monitorInfo = new MonitorInfo();

        monitorInfo.time = getMonitorTime();
        monitorInfo.connectWifiSSID = getConnectWifi();
        monitorInfo.lightLux = lightLux;
        monitorInfo.isWifiEnabled = isWifiEnabled();
        monitorInfo.locRet = baseLocation.getLocRet();

        LogcatHelper.getInstance().printLog(monitorInfo.toString());

        return monitorInfo;
    }

    private int getMonitorTime(){
        Calendar calendar = Calendar.getInstance();

        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int min = calendar.get(Calendar.MINUTE);

        int time = hour * 100 + min;

        return time;
    }

    private String getConnectWifi(){
        WifiInfo info = wifiManager.getConnectionInfo();
        if(info == null || TextUtils.equals(info.getSSID(), WIFI_NONE)){
            return null;
        }else{
            return info.getSSID();
        }
    }

    private boolean isWifiEnabled(){
        return wifiManager.isWifiEnabled();
    }

    private SensorEventListener lightSensorListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
                //光线强度
                lightLux = event.values[0];
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };
}
